package com.secxiot.provider.user.controller;

import com.secxiot.common.annotation.Log;
import com.secxiot.common.entity.ResultVo;
import com.secxiot.common.utils.IdWorker;
import com.secxiot.provider.UserApi;
import com.secxiot.provider.system.dto.AppSchedulDto;
import com.secxiot.provider.system.entity.AppPunchCard;
import com.secxiot.provider.system.entity.AppSchedulRecord;
import com.secxiot.provider.system.vo.AppPunchCardVo;
import com.secxiot.provider.system.vo.AppSchedulRecordVo;
import com.secxiot.provider.system.vo.AppSchedulVo;
import com.secxiot.provider.task.vo.TaskStatusVo;
import com.secxiot.provider.user.dto.BindDto;
import com.secxiot.provider.user.dto.UserDto;
import com.secxiot.provider.user.entity.UserAccount;
import com.secxiot.provider.user.entity.UserDeptDict;
import com.secxiot.provider.user.entity.UserDo;
import com.secxiot.provider.user.service.UserService;
import com.secxiot.provider.user.vo.*;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;


@RequestMapping(value = "users")
@RestController
public class UserController implements UserApi {
    @Autowired
    private IdWorker idWorker;

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //@Ignore
    @Log("根据id查询用户信息")
    @GetMapping("/{id}")
    @Override

    public UserDo getUser(@PathVariable(value = "id", required = true) Long id) {
        return userService.getById(id);
    }

    @Log("根据电话号查询用户信息")
    @GetMapping
    @Override
    public UserDo getUserByPhone(@RequestParam(value = "mobile", required = true) String mobile,
                                 @RequestParam(value = "clientType", required = true) String clientType) {

        /*if (phone.equals("")||phone.equals(null)){
            throw  new BaseRuntimeException(ExceptionEnum.POSSWORD_FALSE);
        }else {
           UserDo userDo=userService.getByPhone(phone);
        return userDo;
        }*/
        return userService.getByPhone(mobile, clientType);
    }

    @Log("根据微信openId查询用户信息")
    @GetMapping("/wxOpenid")
    @Override
    public UserDo getUserByWxOpenid(@RequestParam(value = "wxOpenid", required = true) String wxOpenid) {
        return userService.getByWxOpenid(wxOpenid);
    }

    @Log("添加用户信息")
    @PostMapping
    @Override
    public UserDo registry(@RequestBody UserDto userDto) {
        // TODO 用户存在性判断，用户系统信息填充
        UserDo userDo = new UserDo();
        LOG.info("开始执行判断方法");
        //转换类
        BeanUtils.copyProperties(userDto, userDo);
        userDo.setUserId(idWorker.nextId());
        userService.registry(userDo);
        return userDo;
    }

    @Log("添加用户信息--药品追踪记录小程序")
    @PostMapping("/narcoticRegistry")
    @Override
    public UserDo narcoticRegistry(@RequestBody UserDto userDto) {
        // TODO 用户存在性判断，用户系统信息填充
        UserDo userDo = new UserDo();
        LOG.info("开始执行判断方法");
        //转换类
        BeanUtils.copyProperties(userDto, userDo);
        userDo.setUserId(idWorker.nextId());
        userService.narcoticRegistry(userDo);
        return userDo;
    }

    @Log("添加用户信息--SOS小程序")
    @PostMapping("/sosRegistry")
    @Override
    public UserDo sosRegistry(@RequestBody UserDto userDto) {
        UserDo userDo = new UserDo();
        BeanUtils.copyProperties(userDto, userDo);
        userDo.setUserId(idWorker.nextId());
        userService.sosRegistry(userDo);
        return userDo;
    }

    @Log("修改用户信息")
    @PutMapping
    @Override
    public UserDo update(@RequestBody UserDto userDto) {
        UserDo userDo = new UserDo();
        BeanUtils.copyProperties(userDto, userDo);
        userService.update(userDo);
        return userDo;
    }

    @PostMapping("/bind")
    @Override
    public Integer bindUserRel(@RequestBody BindDto bindDto) {
        return userService.bindUserRel(bindDto);
    }

    public List<UserDeptDict> selectUserDept(@RequestParam List<String> list, @RequestParam List<Long> deptId) {
        // TODO 完成业务
        /*return userService.selectUserDept(list, deptId, );*/
        return null;
    }

    @GetMapping("/list")
    @Override
    public List<UserSimpleVo> getUserSimpleList(
            @RequestParam(value = "userId", required = false) Long userId,
            @RequestParam(value = "userType", required = true) List<String> userType,
            @RequestParam(value = "deptId", required = false) Long deptId,
            @RequestParam(value = "directFlag", required = false) Integer directFlag,
            @RequestParam(value = "taskType", required = false) String taskType) {
        return userService.getUserSimpleList(userId, userType, deptId, directFlag, taskType);
    }

    @Override
    @PostMapping("/contact")
    public UserEmergencyContactVo addContact(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "relation", required = true) String relation,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "mobile", required = true) String mobile) {
        return userService.addContact(userId, relation, name, mobile);
    }

    @Override
    public List<PatientVo> getPatientList(Long deptId) {
        return userService.listUserParent(deptId);
    }

    @Override
    @GetMapping("/sosReceiverList")
    public List<UserSimpleVo> listHospitalSOSDoctor(
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "deptId", required = false) Long deptId,
            @RequestParam(value = "taskType", required = true) String taskType) {
        return userService.listHospitalSOSDoctor(hospitalId, deptId, taskType);
    }

    @GetMapping("/contact")
    @Override
    public UserEmergencyContactVo getContact(@RequestParam(value = "userId", required = true) Long userId) {
        return userService.getContact(userId);
    }

    /**
     * 添加考勤记录
     *
     * @param punchCard 打卡对象
     * @return
     */
    @PostMapping("/addPunchCard")
    public Integer addPunchCard(@RequestBody AppPunchCard punchCard) {
        return userService.addPunchCard(punchCard);
    }

    /**
     * 获取考勤记录列表(30天内)
     *
     * @param userId     用户ID
     * @param hospitalId 医院ID
     * @param dateTime   考勤时间
     * @return
     */
    @GetMapping("/punchCardList")
    public List<AppPunchCardVo> getPunchCardList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "dateTime", required = true) Date dateTime) {
        return userService.getPunchCardList(userId, hospitalId, dateTime);
    }

    /**
     * 获取打卡记录
     *
     * @param userId     用户ID
     * @param hospitalId 医院ID
     * @param punchType  打卡类型
     * @param punchTime  打卡时间
     * @return
     */
    @GetMapping("/getPunchCard")
    public AppPunchCardVo getPunchCard(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "punchType", required = true) String punchType,
            @RequestParam(value = "punchTime", required = true) Date punchTime) {
        return userService.getPunchCard(userId, hospitalId, punchType, punchTime);
    }

    /**
     * 添加排班
     *
     * @param appSchedulDto 排班对象
     * @return
     */
    @PostMapping("/schedul/add")
    public Integer addAppSchedul(@RequestBody AppSchedulDto appSchedulDto) throws ParseException {
        return userService.addAppSchedul(appSchedulDto);
    }

    /**
     * 添加排班记录
     *
     * @param appSchedulRecord 排班记录
     * @return
     */
    @PostMapping("/schedul/addRecord")
    public Integer addAppSchedulRecord(@RequestBody AppSchedulRecord appSchedulRecord) {
        return userService.addAppSchedulRecord(appSchedulRecord);
    }

    /**
     * 获取排班列表
     *
     * @param userId     用户ID
     * @param hospitalId 医院ID
     * @return
     */
    @GetMapping("/schedul/List")
    public List<AppSchedulVo> getAppSchedulList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId) {
        return userService.getAppSchedulList(userId, hospitalId);
    }

    /**
     * 获取排班记录列表
     *
     * @param userId     用户ID
     * @param hospitalId 医院ID
     * @param schedulId  排班ID
     * @return
     */
    @GetMapping("/schedul/recordList")
    public List<AppSchedulRecordVo> getAppSchedulRecordList(
            @RequestParam(value = "userId", required = true) Long userId,
            @RequestParam(value = "hospitalId", required = true) Long hospitalId,
            @RequestParam(value = "schedulId", required = true) Long schedulId) {
        return userService.getAppSchedulRecordList(userId, hospitalId, schedulId);
    }

    /**
     * 获取微信对象
     *
     * @param appId 微信AppID
     * @return
     */
    @GetMapping("/app")
    public WxAppVo getWxApp(@RequestParam(value = "appId", required = true) String appId) {
        return userService.getWxApp(appId);
    }

    /**
     * 获取任务接收者当前任务
     *
     * @param receiverId 接收人用户ID
     * @return
     */
    @Override
    public List<TaskStatusVo> getTaskByReceiverId(@Param("receiverId") Long receiverId) {
        return userService.getTaskByReceiverId(receiverId);
    }

    /**
     * 获取分享用户列表
     *
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<UserVo> getPromoteUserList(@RequestParam("userId") Long userId,
                                           @RequestParam("pageNo") Long pageNo,
                                           @RequestParam("pageSize") Long pageSize) {
        return userService.getPromoteUserList(userId, pageNo, pageSize);
    }

    /**
     * 绑定分享码
     *
     * @param userId      用户ID
     * @param promoteCode 邀请码
     * @return
     */
    @Override
    public Integer bingPromoteCode(@RequestParam("userId") Long userId,
                                   @RequestParam("promoteCode") String promoteCode) {
        return userService.bingPromoteCode(userId, promoteCode);
    }

    /**
     * 绑定用户提现账号
     *
     * @param userId     用户ID
     * @param type       账户类型
     * @param payAccount 账号
     * @return
     */
    @Override
    public Integer bingUserAccount(@RequestParam(value = "userId") Long userId,
                                   @RequestParam(value = "type") Integer type,
                                   @RequestParam(value = "payAccount") String payAccount) {
        return userService.bingUserAccount(userId, type, payAccount);
    }

    /**
     * 编辑用户提现账号
     *
     * @param userId     用户ID
     * @param type       账户类型
     * @param payAccount 账号
     * @return
     */
    @Override
    public Integer editUserAccount(@RequestParam(value = "userId") Long userId,
                                   @RequestParam(value = "type") Integer type,
                                   @RequestParam(value = "payAccount") String payAccount) {
        return userService.editUserAccount(userId, type, payAccount);
    }

    @Override
    public UserAccount getUserAccount(@RequestParam(value = "userId") Long userId) {
        return userService.getUserAccount(userId);
    }
}
